#include <iostream>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
int main() {
    priority_queue<ll,vector<ll>,greater<ll>> q;
    int n;
    cin >> n;
    while(n--)
    {
        int x;
        cin >> x;
        q.push(x);
    }
    ll sum=0;
    while(q.size()>1)
    {
        ll t1=q.top();
        q.pop();
        ll t2=q.top();
        q.pop();
        sum+=(t1+t2);
        q.push(t1+t2);
    }
    cout << sum;
    return 0;
}